package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.BookingPolicyLogic;

import data.BookingPolicy;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class BookingPolicyService extends HttpServlet{
	//http://localhost:8080/bookingpolicyService?func=addBookingPolicy&title=1&isDay=true&Percentcharge=true&fix=true&type=D&beforeArrival=5&charge=2&chargeNight=true
	//http://localhost:8080/bookingpolicyService?func=editBookingPolicy&bookingpolicyId=879&title=122222222222222&isDay=true&Percentcharge=true&fix=true&type=D&beforeArrival=5&charge=2&chargeNight=true
	//http://localhost:8080/bookingpolicyService?func=delBookingPolicy&bookingpolicyId=879
	//http://localhost:8080/bookingpolicyService?func=changeStatus&bookingpolicyId=879
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("addBookingPolicy")){
				AddBookingPolicy(req,resp);
			}else if(doClass.equals("editBookingPolicy")){
				EditBookingPolicy(req,resp);
			}else if(doClass.equals("delBookingPolicy")){
				DeleteBookingPolicy(req,resp);
			}else if(doClass.equals("getAllBookingPolicy4Admin")){
				GetAllBookingPolicy4Admin(req,resp);
			}else if(doClass.equals("getBookingPolicyById")){
				GetBookingPolicyById(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void AddBookingPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		boolean isDay = Boolean.parseBoolean(req.getParameter("isDay"));
		String type = req.getParameter("type");
		boolean Percentcharge = Boolean.parseBoolean(req.getParameter("Percentcharge"));
		boolean fix = Boolean.parseBoolean(req.getParameter("fix"));
		boolean chargeNight  = Boolean.parseBoolean(req.getParameter("chargeNight"));
		double charge = Double.parseDouble(req.getParameter("charge"));
		long beforeArrival = Long.parseLong(req.getParameter("beforeArrival"));

		new BookingPolicyLogic().AddBookingPolicy(hotelId,title,isDay,Percentcharge,fix,type,beforeArrival,charge,chargeNight);

		GetAllBookingPolicy4Admin(req, resp);
	}

	private void EditBookingPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long bookingpolicyId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		boolean isDay = Boolean.parseBoolean(req.getParameter("isDay"));
		String type = req.getParameter("type");
		boolean Percentcharge = Boolean.parseBoolean(req.getParameter("Percentcharge"));
		boolean fix = Boolean.parseBoolean(req.getParameter("fix"));
		boolean chargeNight  = Boolean.parseBoolean(req.getParameter("chargeNight"));
		double charge = Double.parseDouble(req.getParameter("charge"));
		long beforeArrival = Long.parseLong(req.getParameter("beforeArrival"));
				
		new BookingPolicyLogic().EditBookingPolicy(bookingpolicyId,title,isDay,Percentcharge,fix,type,beforeArrival,charge,chargeNight);
		
		GetAllBookingPolicy4Admin(req, resp);
	}

	private void DeleteBookingPolicy(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		
		for(int i = 0; i < id.split(",").length ; i++){
			new BookingPolicyLogic().DeleteBookingPolicy(Long.parseLong(id.split(",")[i]));
		}

		GetAllBookingPolicy4Admin(req, resp);
	}
	
	private void GetAllBookingPolicy4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");

		long line = Long.parseLong(req.getParameter("line"));
		long offset = Long.parseLong(req.getParameter("offset"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		List<BookingPolicy> obj = new BookingPolicyLogic().GetAllBookingPolicy4AdminLimit(hotelId,offset,line);
		double dataSize = new BookingPolicyLogic().GetAllBookingPolicy(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetBookingPolicyById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		BookingPolicy c = new BookingPolicyLogic().GetBookingPolicy(id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(c));
	}
}
